//
//  CityViewController.m
//  SelfDriving
//
//  Created by apple on 16/2/23.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CityViewController.h"
#import "CityModel.h"
#import "BusinessService+User.h"
@interface CityViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UITableView *table1;
@property (weak, nonatomic) IBOutlet UITableView *table2;
@property (weak, nonatomic) IBOutlet UILabel *tile;
@property (weak, nonatomic) IBOutlet UIButton *okBtn;
@property (weak, nonatomic) IBOutlet UIButton *qxBtn;

@end

@implementation CityViewController{
    NSArray *data1;
    NSArray *data2;
    int index1;
    int index2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    _bgView.frame = self.view.frame;
    _bgView.layer.opacity = 0.5;
    _mainView.layer.cornerRadius = 4;
    
    _tile.layer.cornerRadius = 8;
    _okBtn.layer.cornerRadius = 4;
    _qxBtn.layer.cornerRadius = 4;
    
    _table1.showsHorizontalScrollIndicator = NO;
    _table1.showsVerticalScrollIndicator = NO;
    _table2.showsHorizontalScrollIndicator = NO;
    _table2.showsVerticalScrollIndicator = NO;
    [self initdata];
}

-(void)initdata{
    
    [[BusinessService shareInstance]getCity:[[NSMutableDictionary alloc]init] success:^(id result) {
        data1 = result;
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        CityModel *model = [data1 firstObject];
        [dic setObject:model.id forKey:@"parentId"];
        [[BusinessService shareInstance]getCity:dic success:^(id result) {
            data2 = result;
            [_table1 reloadData];
            [_table2 reloadData];
        } failure:^(id result) {
        }];
    } failure:^(id result) {
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag == 1991) {
        return data1.count;
    }
    if (tableView.tag == 1994) {
        return data2.count;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    if (tableView.tag == 1991) {
        CityModel *model = [data1 objectAtIndex:indexPath.row];
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 45)];
        lable.font = [UIFont systemFontOfSize:16];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.text = model.name;
        [cell.contentView addSubview:lable];
        if (index1 == indexPath.row) {
            cell.backgroundColor = UIColorFromRGB(0xf1f1f1);
        }
    }
    if (tableView.tag == 1994) {
        CityModel *model = [data2 objectAtIndex:indexPath.row];
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 45)];
        lable.font = [UIFont systemFontOfSize:16];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.text = model.name;
        [cell.contentView addSubview:lable];
        if (index2 == indexPath.row) {
            cell.backgroundColor = UIColorFromRGB(0xf1f1f1);
        }
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 1991) {
        index1 = (int)indexPath.row;
        index2 = 0;
        [_table1 reloadData];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        CityModel *model = [data1 objectAtIndex:indexPath.row];
        [dic setObject:model.id forKey:@"parentId"];
        [[BusinessService shareInstance]getCity:dic success:^(id result) {
            data2 = result;
            [_table2 reloadData];
        } failure:^(id result) {
        }];
    }else{
        index2 = (int)indexPath.row;
        [_table2 reloadData];
        
    }
}

- (IBAction)okAction:(UIButton *)sender {
    CityModel *model1 = [data1 objectAtIndex:index1];

    CityModel *model = [data2 objectAtIndex:index2];
    NSString *name = [NSString stringWithFormat:@"%@-%@",model1.name,model.name];
    [self.delegate didSelectDate:name id:model.id];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)backAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
